#include <stdio.h>
#include <string.h>
#include "student.h"

extern Student students[]; // 从fileops.c引入
extern int current_student_count; // 从fileops.c引入
#define MAX_STUDENTS 100

void addStudent() {
    if (current_student_count >= MAX_STUDENTS) {
        printf("达到最大学生数量限制!\n");
        return;
    }

    printf("请输入学生信息:\n");

    printf("学号: ");
    scanf("%d", &students[current_student_count].id);
    getchar(); // 清除缓冲区

    printf("姓名: ");
    fgets(students[current_student_count].name, 50, stdin);
    students[current_student_count].name[strcspn(students[current_student_count].name, "\n")] = 0; // 去除换行符

    printf("性别: ");
    fgets(students[current_student_count].gender, 10, stdin);
    students[current_student_count].gender[strcspn(students[current_student_count].gender, "\n")] = 0;

    printf("年龄: ");
    scanf("%d", &students[current_student_count].age);
    getchar();

    printf("班级: ");
    fgets(students[current_student_count].class, 50, stdin);
    students[current_student_count].class[strcspn(students[current_student_count].class, "\n")] = 0;

    printf("联系方式: ");
    fgets(students[current_student_count].contact, 50, stdin);
    students[current_student_count].contact[strcspn(students[current_student_count].contact, "\n")] = 0;

    current_student_count++;
    printf("学生信息已添加成功!\n");
}

void modifyStudent() {
    int id;
    printf("请输入要修改的学生学号: ");
    scanf("%d", &id);
    getchar();

    for (int i = 0; i < current_student_count; i++) {
        if (students[i].id == id) {
            printf("找到学生，开始修改...\n");

            printf("姓名 (%s): ", students[i].name);
            fgets(students[i].name, 50, stdin);
            students[i].name[strcspn(students[i].name, "\n")] = 0;

            printf("性别 (%s): ", students[i].gender);
            fgets(students[i].gender, 10, stdin);
            students[i].gender[strcspn(students[i].gender, "\n")] = 0;

            printf("年龄 (%d): ", students[i].age);
            scanf("%d", &students[i].age);
            getchar();

            printf("班级 (%s): ", students[i].class);
            fgets(students[i].class, 50, stdin);
            students[i].class[strcspn(students[i].class, "\n")] = 0;

            printf("联系方式 (%s): ", students[i].contact);
            fgets(students[i].contact, 50, stdin);
            students[i].contact[strcspn(students[i].contact, "\n")] = 0;

            printf("学生信息已修改成功!\n");
            return;
        }
    }
    printf("未找到该学号的学生。\n");
}

void deleteStudent() {
    int id;
    printf("请输入要删除的学生学号: ");
    scanf("%d", &id);

    int index = -1;
    for (int i = 0; i < current_student_count; i++) {
        if (students[i].id == id) {
            index = i;
            break;
        }
    }

    if (index != -1) {
        for (int i = index; i < current_student_count - 1; i++) {
            students[i] = students[i + 1];
        }
        current_student_count--;
        printf("学生信息已删除!\n");
    } else {
        printf("未找到该学号的学生。\n");
    }
}

void queryStudent() {
    int id;
    printf("请输入要查询的学生学号: ");
    scanf("%d", &id);

    for (int i = 0; i < current_student_count; i++) {
        if (students[i].id == id) {
            printf("学号: %d\n", students[i].id);
            printf("姓名: %s\n", students[i].name);
            printf("性别: %s\n", students[i].gender);
            printf("年龄: %d\n", students[i].age);
            printf("班级: %s\n", students[i].class);
            printf("联系方式: %s\n", students[i].contact);
            return;
        }
    }
    printf("未找到该学号的学生。\n");
}

void displayAllStudents() {
    printf("当前所有学生信息:\n");
    for (int i = 0; i < current_student_count; i++) {
        printf("------\n");
        printf("学号: %d\n", students[i].id);
        printf("姓名: %s\n", students[i].name);
        printf("性别: %s\n", students[i].gender);
        printf("年龄: %d\n", students[i].age);
        printf("班级: %s\n", students[i].class);
        printf("联系方式: %s\n", students[i].contact);
    }
}
